﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using SPMS.Commons.Logging.Properties;

namespace SPMS.Commons.Logging
{
    /// <summary>
    /// 抽象日志类
    /// </summary>
    /// <remarks>提供了默认的日志接口实现基类</remarks>
    /// <history>
    /// [zanezeng]   2008-11-2 10:56    Created
    /// </history>
    public abstract class AbstractLog : ILog
    {
        #region ---Constructor

        /// <summary>
        /// 初始化类型<see cref="AbstractLog"/>的新实例
        /// </summary>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:57    创建
        /// </history>
        protected AbstractLog()
            : this( LogLevelEnum.All )
        {
        }

        /// <summary>
        /// 初始化类型<see cref="AbstractLog"/>的新实例
        /// </summary>
        /// <param name="level">关联到当期日志记录器上的日志级别</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:57    创建
        /// </history>
        protected AbstractLog( LogLevelEnum level )
        {
            //记录日志级别
            this.m_Level = level;
        }

        #endregion

        #region ---ILog

        #region ---Property

        /// <summary>
        /// 根据当前日志级别设定判断是否允许输出调试级别日志信息
        /// </summary>
        /// <value>
        /// 	<c>true</c>允许<br/>
        /// 	<c>false</c>不允许
        /// </value>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:23 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:40    创建
        /// </history>
        public bool IsDebugEnabled
        {
            get
            {
                return this.IsLevelEanbled( LogLevelEnum.Debug );
            }
        }

        /// <summary>
        /// 根据当前日志级别设定判断是否允许输出跟踪级别日志信息
        /// </summary>
        /// <value>
        /// 	<c>true</c>允许<br/>
        /// 	<c>false</c>不允许
        /// </value>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:19 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:40    创建
        /// </history>
        public bool IsTraceEnabled
        {
            get
            {
                return this.IsLevelEanbled( LogLevelEnum.Trace );
            }
        }

        /// <summary>
        /// 根据当前日志级别设定判断是否允许输出提示级别日志信息
        /// </summary>
        /// <value>
        /// 	<c>true</c>允许<br/>
        /// 	<c>false</c>不允许
        /// </value>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:24 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:41    创建
        /// </history>
        public bool IsInfoEnabled
        {
            get
            {
                return this.IsLevelEanbled( LogLevelEnum.Info );
            }
        }

        /// <summary>
        /// 根据当前日志级别设定判断是否允许输出警告级别日志信息
        /// </summary>
        /// <value>
        /// 	<c>true</c>允许<br/>
        /// 	<c>false</c>不允许
        /// </value>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:25 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:41    创建
        /// </history>
        public bool IsWarnEnabled
        {
            get
            {
                return this.IsLevelEanbled( LogLevelEnum.Warn );
            }
        }

        /// <summary>
        /// 根据当前日志级别设定判断是否允许输出错误级别日志信息
        /// </summary>
        /// <value>
        /// 	<c>true</c>允许<br/>
        /// 	<c>false</c>不允许
        /// </value>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:25 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:41    创建
        /// </history>
        public bool IsErrorEnabled
        {
            get
            {
                return this.IsLevelEanbled( LogLevelEnum.Error );
            }
        }

        /// <summary>
        /// 根据当前日志级别设定判断是否允许输出灾难级别日志信息
        /// </summary>
        /// <value>
        /// 	<c>true</c>允许<br/>
        /// 	<c>false</c>不允许
        /// </value>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:25 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:42    创建
        /// </history>
        public bool IsFatalEnabled
        {
            get
            {
                return this.IsLevelEanbled( LogLevelEnum.Fatal );
            }
        }

        #endregion

        /// <summary>
        /// 判断当前日志记录器是否允许输出给定日志级别的日志
        /// </summary>
        /// <param name="level">用于判定的日志级别</param>
        /// <returns>
        /// 	<c>true</c>：允许输出<br/>
        /// 	<c>false</c>：不允许输出
        /// </returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:30    创建
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:42    创建
        /// </history>
        public virtual bool IsLevelEanbled( LogLevelEnum level )
        {
            //比较日志级别
            return level >= this.m_Level;
        }

        /// <summary>
        /// 输出日志信息
        /// </summary>
        /// <param name="level">要输出的日志信息的级别</param>
        /// <param name="message">需要输出到日志的信息</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:42    创建
        /// </history>
        public void WriteLog( LogLevelEnum level, object message )
        {
            //判断是否允许输出
            if (!IsLevelEanbled( level ))
            {
                //如果不允许输出，则直接返回
                return;
            }

            //输出日志
            this.SafeWriteLog( level, null, message );
        }

        /// <summary>
        /// 输出日志信息
        /// </summary>
        /// <param name="level">要输出的日志信息的级别</param>
        /// <param name="ex">需要输出到日志的异常对象</param>
        /// <param name="message">需要输出到日志的信息</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:46    创建
        /// </history>
        public void WriteLog( LogLevelEnum level, Exception ex, object message )
        {
            //判断是否允许输出
            if (!IsLevelEanbled( level ))
            {
                //如果不允许输出，则直接返回
                return;
            }

            //输出日志
            this.SafeWriteLog( level, ex, message );
        }

        /// <summary>
        /// 输出日志信息
        /// </summary>
        /// <param name="level">要输出的日志信息的级别</param>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="args">输出信息的格式化参数</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:46    创建
        /// </history>
        public void WriteLog( LogLevelEnum level, string message, params object[] args )
        {
            //判断是否允许输出
            if (!IsLevelEanbled( level ))
            {
                //如果不允许输出，则直接返回
                return;
            }

            //格式化消息
            message = this.FormatMessage( null, message, args );

            //输出日志
            this.SafeWriteLog( level, null, message );
        }

        /// <summary>
        /// 输出日志信息
        /// </summary>
        /// <param name="level">要输出的日志信息的级别</param>
        /// <param name="ex">需要输出到日志的异常对象</param>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="args">输出信息的格式化参数</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:47    创建
        /// </history>
        public void WriteLog( LogLevelEnum level, Exception ex, string message, params object[] args )
        {
            //判断是否允许输出
            if (!IsLevelEanbled( level ))
            {
                //如果不允许输出，则直接返回
                return;
            }

            //格式化消息
            message = this.FormatMessage( null, message, args );

            //输出日志
            this.SafeWriteLog( level, ex, message );
        }

        /// <summary>
        /// 输出日志信息
        /// </summary>
        /// <param name="level">要输出的日志信息的级别</param>
        /// <param name="provider">用于消息格式化的格式化提供者</param>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="args">输出信息的格式化参数</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:48    创建
        /// </history>
        public void WriteLog( LogLevelEnum level, IFormatProvider provider, string message, params object[] args )
        {
            //判断是否允许输出
            if (!IsLevelEanbled( level ))
            {
                //如果不允许输出，则直接返回
                return;
            }

            //格式化消息
            message = this.FormatMessage( provider, message, args );

            //输出日志
            this.SafeWriteLog( level, null, message );
        }

        /// <summary>
        /// 输出日志信息
        /// </summary>
        /// <param name="level">要输出的日志信息的级别</param>
        /// <param name="ex">需要输出到日志的异常对象</param>
        /// <param name="provider">用于消息格式化的格式化提供者</param>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="args">输出信息的格式化参数</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:48    创建
        /// </history>
        public void WriteLog( LogLevelEnum level, Exception ex, IFormatProvider provider, string message, params object[] args )
        {
            //判断是否允许输出
            if (!IsLevelEanbled( level ))
            {
                //如果不允许输出，则直接返回
                return;
            }

            //格式化消息
            message = this.FormatMessage( provider, message, args );

            //输出日志
            this.SafeWriteLog( level, ex, message );
        }

        /// <summary>
        /// 输出调试信息
        /// </summary>
        /// <param name="message">需要输出到日志的信息</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:49    创建
        /// </history>
        public void Debug( object message )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Debug, message );
        }

        /// <summary>
        /// 输出调试信息
        /// </summary>
        /// <param name="ex">需要输出到日志的异常对象</param>
        /// <param name="message">需要输出到日志的信息</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:50    创建
        /// </history>
        public void Debug( Exception ex, object message )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Debug, ex, message );
        }

        /// <summary>
        /// 输出调试信息
        /// </summary>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="args">输出信息的格式化参数</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:50    创建
        /// </history>
        public void Debug( string message, params object[] args )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Debug, message, args );
        }

        /// <summary>
        /// 输出调试信息
        /// </summary>
        /// <param name="ex">需要输出到日志的异常对象</param>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="args">输出信息的格式化参数</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:50    创建
        /// </history>
        public void Debug( Exception ex, string message, params object[] args )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Debug, ex, message, args );
        }

        /// <summary>
        /// 输出调试信息
        /// </summary>
        /// <param name="provider">用于消息格式化的格式化提供者</param>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="args">输出信息的格式化参数</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:50    创建
        /// </history>
        public void Debug( IFormatProvider provider, string message, params object[] args )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Debug, provider, message, args );
        }

        /// <summary>
        /// 输出调试信息
        /// </summary>
        /// <param name="ex">需要输出到日志的异常对象</param>
        /// <param name="provider">用于消息格式化的格式化提供者</param>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="args">输出信息的格式化参数</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:50    创建
        /// </history>
        public void Debug( Exception ex, IFormatProvider provider, string message, params object[] args )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Debug, ex, provider, message, args );
        }

        /// <summary>
        /// 输出跟踪信息
        /// </summary>
        /// <param name="message">需要输出到日志的信息</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:51    创建
        /// </history>
        public void Trace( object message )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Trace, message );
        }

        /// <summary>
        /// 输出跟踪信息
        /// </summary>
        /// <param name="ex">需要输出到日志的异常对象</param>
        /// <param name="message">需要输出到日志的信息</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:51    创建
        /// </history>
        public void Trace( Exception ex, object message )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Trace, ex, message );
        }

        /// <summary>
        /// 输出跟踪信息
        /// </summary>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="args">输出信息的格式化参数</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:51    创建
        /// </history>
        public void Trace( string message, params object[] args )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Trace, message, args );
        }

        /// <summary>
        /// 输出跟踪信息
        /// </summary>
        /// <param name="ex">需要输出到日志的异常对象</param>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="args">输出信息的格式化参数</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:51    创建
        /// </history>
        public void Trace( Exception ex, string message, params object[] args )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Trace, ex, message, args );
        }

        /// <summary>
        /// 输出跟踪信息
        /// </summary>
        /// <param name="provider">用于消息格式化的格式化提供者</param>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="args">输出信息的格式化参数</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:51    创建
        /// </history>
        public void Trace( IFormatProvider provider, string message, params object[] args )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Trace, provider, message, args );
        }

        /// <summary>
        /// 输出跟踪信息
        /// </summary>
        /// <param name="ex">需要输出到日志的异常对象</param>
        /// <param name="provider">用于消息格式化的格式化提供者</param>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="args">输出信息的格式化参数</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:52    创建
        /// </history>
        public void Trace( Exception ex, IFormatProvider provider, string message, params object[] args )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Trace, ex, provider, message, args );
        }

        /// <summary>
        /// 输出提示信息
        /// </summary>
        /// <param name="message">需要输出到日志的信息</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:52    创建
        /// </history>
        public void Info( object message )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Info, message );
        }

        /// <summary>
        /// 输出提示信息
        /// </summary>
        /// <param name="ex">需要输出到日志的异常对象</param>
        /// <param name="message">需要输出到日志的信息</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:52    创建
        /// </history>
        public void Info( Exception ex, object message )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Info, ex, message );
        }

        /// <summary>
        /// 输出提示信息
        /// </summary>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="args">输出信息的格式化参数</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:52    创建
        /// </history>
        public void Info( string message, params object[] args )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Info, message, args );
        }

        /// <summary>
        /// 输出提示信息
        /// </summary>
        /// <param name="ex">需要输出到日志的异常对象</param>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="args">输出信息的格式化参数</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:52    创建
        /// </history>
        public void Info( Exception ex, string message, params object[] args )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Info, ex, message, args );
        }

        /// <summary>
        /// 输出提示信息
        /// </summary>
        /// <param name="provider">用于消息格式化的格式化提供者</param>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="args">输出信息的格式化参数</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:53    创建
        /// </history>
        public void Info( IFormatProvider provider, string message, params object[] args )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Info, provider, message, args );
        }

        /// <summary>
        /// 输出提示信息
        /// </summary>
        /// <param name="ex">需要输出到日志的异常对象</param>
        /// <param name="provider">用于消息格式化的格式化提供者</param>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="args">输出信息的格式化参数</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:53    创建
        /// </history>
        public void Info( Exception ex, IFormatProvider provider, string message, params object[] args )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Info, ex, provider, message, args );
        }

        /// <summary>
        /// 输出警告信息
        /// </summary>
        /// <param name="message">需要输出到日志的信息</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:53    创建
        /// </history>
        public void Warn( object message )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Warn, message );
        }

        /// <summary>
        /// 输出警告信息
        /// </summary>
        /// <param name="ex">需要输出到日志的异常对象</param>
        /// <param name="message">需要输出到日志的信息</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:54    创建
        /// </history>
        public void Warn( Exception ex, object message )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Warn, ex, message );
        }

        /// <summary>
        /// 输出警告信息
        /// </summary>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="args">输出信息的格式化参数</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:54    创建
        /// </history>
        public void Warn( string message, params object[] args )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Warn, message, args );
        }

        /// <summary>
        /// 输出警告信息
        /// </summary>
        /// <param name="ex">需要输出到日志的异常对象</param>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="args">输出信息的格式化参数</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:54    创建
        /// </history>
        public void Warn( Exception ex, string message, params object[] args )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Warn, ex, message, args );
        }

        /// <summary>
        /// 输出警告信息
        /// </summary>
        /// <param name="provider">用于消息格式化的格式化提供者</param>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="args">输出信息的格式化参数</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:54    创建
        /// </history>
        public void Warn( IFormatProvider provider, string message, params object[] args )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Warn, provider, message, args );
        }

        /// <summary>
        /// 输出警告信息
        /// </summary>
        /// <param name="ex">需要输出到日志的异常对象</param>
        /// <param name="provider">用于消息格式化的格式化提供者</param>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="args">输出信息的格式化参数</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:54    创建
        /// </history>
        public void Warn( Exception ex, IFormatProvider provider, string message, params object[] args )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Warn, ex, provider, message, args );
        }

        /// <summary>
        /// 输出错误信息
        /// </summary>
        /// <param name="message">需要输出到日志的信息</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:54    创建
        /// </history>
        public void Error( object message )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Error, message );
        }

        /// <summary>
        /// 输出错误信息
        /// </summary>
        /// <param name="ex">需要输出到日志的异常对象</param>
        /// <param name="message">需要输出到日志的信息</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:55    创建
        /// </history>
        public void Error( Exception ex, object message )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Error, ex, message );
        }

        /// <summary>
        /// 输出错误信息
        /// </summary>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="args">输出信息的格式化参数</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:55    创建
        /// </history>
        public void Error( string message, params object[] args )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Error, message, args );
        }

        /// <summary>
        /// 输出错误信息
        /// </summary>
        /// <param name="ex">需要输出到日志的异常对象</param>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="args">输出信息的格式化参数</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:55    创建
        /// </history>
        public void Error( Exception ex, string message, params object[] args )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Error, ex, message, args );
        }

        /// <summary>
        /// 输出错误信息
        /// </summary>
        /// <param name="provider">用于消息格式化的格式化提供者</param>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="args">输出信息的格式化参数</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:55    创建
        /// </history>
        public void Error( IFormatProvider provider, string message, params object[] args )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Error, provider, message, args );
        }

        /// <summary>
        /// 输出错误信息
        /// </summary>
        /// <param name="ex">需要输出到日志的异常对象</param>
        /// <param name="provider">用于消息格式化的格式化提供者</param>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="args">输出信息的格式化参数</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:55    创建
        /// </history>
        public void Error( Exception ex, IFormatProvider provider, string message, params object[] args )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Error, ex, provider, message, args );
        }

        /// <summary>
        /// 输出灾难信息
        /// </summary>
        /// <param name="message">需要输出到日志的信息</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:55    创建
        /// </history>
        public void Fatal( object message )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Fatal, message );
        }

        /// <summary>
        /// 输出灾难信息
        /// </summary>
        /// <param name="ex">需要输出到日志的异常对象</param>
        /// <param name="message">需要输出到日志的信息</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:55    创建
        /// </history>
        public void Fatal( Exception ex, object message )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Fatal, ex, message );
        }

        /// <summary>
        /// 输出灾难信息
        /// </summary>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="args">输出信息的格式化参数</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:56    创建
        /// </history>
        public void Fatal( string message, params object[] args )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Fatal, message, args );
        }

        /// <summary>
        /// 输出灾难信息
        /// </summary>
        /// <param name="ex">需要输出到日志的异常对象</param>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="args">输出信息的格式化参数</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:56    创建
        /// </history>
        public void Fatal( Exception ex, string message, params object[] args )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Fatal, ex, message, args );
        }

        /// <summary>
        /// 输出灾难信息
        /// </summary>
        /// <param name="provider">用于消息格式化的格式化提供者</param>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="args">输出信息的格式化参数</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:56    创建
        /// </history>
        public void Fatal( IFormatProvider provider, string message, params object[] args )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Fatal, provider, message, args );
        }

        /// <summary>
        /// 输出灾难信息
        /// </summary>
        /// <param name="ex">需要输出到日志的异常对象</param>
        /// <param name="provider">用于消息格式化的格式化提供者</param>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="args">输出信息的格式化参数</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:56    创建
        /// </history>
        public void Fatal( Exception ex, IFormatProvider provider, string message, params object[] args )
        {
            //输出日志
            this.WriteLog( LogLevelEnum.Fatal, ex, provider, message, args );
        }

        #endregion

        #region ---Protected Method

        /// <summary>
        /// 输出日志信息
        /// </summary>
        /// <param name="level">要输出的日志信息的级别</param>
        /// <param name="ex">需要输出到日志的异常对象</param>
        /// <param name="message">需要输出到日志的信息</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:44    创建
        /// </history>
        protected abstract void WriteLogInternal( LogLevelEnum level, Exception ex, object message );

        /// <summary>
        /// 格式化字符串消息
        /// </summary>
        /// <param name="provider">字符串格式化提供者</param>
        /// <param name="message">消息的复合格式字符串</param>
        /// <param name="args">包含零个或多个要格式化的对象的<see cref="System.Object"/>数组</param>
        /// <returns><paramref name="message"/>的一个副本，其中的第一个格式项已替换为<paramref name="args"/>中相应<see cref="System.Object"/>实例的<see cref="System.String"/>等效项</returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:47    创建
        /// </history>
        protected virtual string FormatMessage( IFormatProvider provider, string message, params object[] args )
        {
            try
            {
                //判断要格式化的消息是否为NULL或者为空字符串
                if (string.IsNullOrEmpty( message ))
                {
                    //如果要格式化的消息为NULL或者为空字符串，则不需要进行格式化
                    return message;
                }

                //判断是否提供了字符串格式化提供者
                if (null == provider)
                {
                    //格式化字符串
                    return string.Format( message, args );
                }
                else
                {
                    //使用格式化提供器格式化
                    return string.Format( provider, message, args );
                }
            }
            catch (Exception ex)
            {
                //输出日志
                if (LOG.IsErrorEnabled)
                {
                    LOG.Error( ex, "格式化日志消息'{0}'的过程中发生异常，格式化日志消息失败", message );
                }

                //返回消息模板
                return message;
            }
        }

        /// <summary>
        /// 获得日志级别对应的显示名称
        /// </summary>
        /// <param name="level">要获得显示名称的日志级别</param>
        /// <returns>日志级别对应的显示名称</returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 14:28    创建
        /// </history>
        protected static string GetLogLevelDisplayName( LogLevelEnum level )
        {
            //根据日志级别的不同返回现实名称
            switch (level)
            {
                //调试级别
                case LogLevelEnum.Debug:
                    return Logging.Properties.Resources.MSG_LOG_NAME_DEBUG;

                //跟踪级别
                case LogLevelEnum.Trace:
                    return Logging.Properties.Resources.MSG_LOG_NAME_TRACE;

                //提示级别
                case LogLevelEnum.Info:
                    return Logging.Properties.Resources.MSG_LOG_NAME_INFO;

                //警告级别
                case LogLevelEnum.Warn:
                    return Logging.Properties.Resources.MSG_LOG_NAME_WARN;

                //错误级别
                case LogLevelEnum.Error:
                    return Logging.Properties.Resources.MSG_LOG_NAME_ERROR;

                //灾难级别
                case LogLevelEnum.Fatal:
                    return Logging.Properties.Resources.MSG_LOG_NAME_FATAL;

                //默认为空字符串
                default:
                    return string.Empty;
            }
        }

        /// <summary>
        /// 获得给定堆栈分界类型之后的调用堆栈
        /// </summary>
        /// <param name="stackBoundaryType">调用堆栈分界类型</param>
        /// <returns>调用堆栈</returns>
        /// <remarks></remarks>
        /// <exception cref="ArgumentNullException"><paramref name="stackBoundaryType"/>为Null</exception>
        /// <history>
        /// [zanezeng]               2009/9/29 9:58    创建
        /// </history>
        protected static StackFrame GetLastStackFrame( Type stackBoundaryType )
        {
            //创建调用堆栈跟踪
            StackTrace trace = new StackTrace( true );

            //判断是否有分界类型
            if (null == stackBoundaryType)
            {
                //如果没有，则返回第一帧
                if (trace.FrameCount > 0)
                {
                    return trace.GetFrame( 0 );
                }
                else
                {
                    return null;
                }
            }

            //用于保存调用帧序号
            int index = trace.FrameCount - 1;

            //用于保存调用帧
            StackFrame frame = null;

            //获得分界类型开始的第一个调用帧
            while (index >= 0)
            {
                //获得调用帧
                frame = trace.GetFrame( index );

                //判断是否为分界类型的调用帧
                if (null != frame && frame.GetMethod().ReflectedType == stackBoundaryType)
                {
                    //中断循环
                    break;
                }

                //减少序号
                index--;
            }

            //获得第一个不是分界类型的调用帧
            while (index < trace.FrameCount)
            {
                //获得调用帧
                frame = trace.GetFrame( index );

                //判断是否为分界类型的调用帧
                if (null != frame && frame.GetMethod().ReflectedType != stackBoundaryType)
                {
                    //返回调用帧
                    return frame;
                }

                //增加序号
                index++;
            }

            //默认返回NULL
            return null;
        }

        #endregion

        #region ---Private Method

        /// <summary>
        /// 安全输出日志信息
        /// </summary>
        /// <param name="level">要输出的日志信息的级别</param>
        /// <param name="ex">需要输出到日志的异常对象</param>
        /// <param name="message">需要输出到日志的信息</param>
        /// <remarks>不抛出异常</remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 13:44    创建
        /// </history>
        private void SafeWriteLog( LogLevelEnum level, Exception ex, object message )
        {
            try
            {
                //写入日志信息
                this.WriteLogInternal( level, ex, message );
            }
            catch (Exception exception)
            {
                //输出日志
                if (LOG.IsErrorEnabled)
                {
                    LOG.Error( exception, "写入日志信息的过程中发生异常，写入日志信息失败" );
                }
            }
        }

        #endregion

        #region ---Property

        /// <summary>
        /// 获得或者设置当前日志记录器关联的日志级别
        /// </summary>
        /// <value></value>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/12/22 9:14    创建
        /// </history>
        public LogLevelEnum Level
        {
            get
            {
                return this.m_Level;
            }
            set
            {
                this.m_Level = value;
            }
        }

        #endregion

        #region ---Field

        /// <summary>
        /// 关联到当期日志记录器上的日志级别
        /// </summary>
        private LogLevelEnum m_Level;

        /// <summary>
        /// 元日志
        /// </summary>
        private readonly static ILog LOG = SimpleLogFactory.Default.GetLog( "SPMS.Commons.Logging" );

        #endregion
    }
}
